﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.EndModelingEntityLevel.CApEMEndModelingEntityLevelApp
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using EngineeringInternalExtension.Options;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.EndModelingEntityLevel;

internal class CApEMEndModelingEntityLevelApp
{
  private ITxComponent m_component;
  private Dictionary<ITxDetailedRepresentation, bool> m_refrencedEntities = new Dictionary<ITxDetailedRepresentation, bool>();
  private bool m_reloadInDetailRepresentation;
  private bool m_reloadPMIs;
  private bool m_reloadWithCommand;
  private bool m_considerNonModelingStateWithoutUI;

  public CApEMEndModelingEntityLevelApp()
  {
  }

  public CApEMEndModelingEntityLevelApp(ITxComponent component)
  {
    this.m_component = component;
    if (this.m_component != null)
      this.InitComponentReferencedData(this.m_component.GetReferencedEntities(new TxObjectList()), this.m_refrencedEntities);
    this.m_reloadInDetailRepresentation = false;
    this.m_reloadPMIs = this.IsNonModelingPMILoaded;
  }

  private void InitComponentReferencedData(
    TxObjectList referencedEntities,
    Dictionary<ITxDetailedRepresentation, bool> dataToInit)
  {
    foreach (ITxDetailedRepresentation referencedEntity in (Collection<ITxObject>) referencedEntities)
    {
      if (referencedEntity != null && !referencedEntity.Preserved)
        dataToInit.Add(referencedEntity, false);
    }
  }

  public virtual bool ComponentEndModeling()
  {
    bool flag = false;
    if (this.m_considerNonModelingStateWithoutUI)
    {
      this.ReloadInDetailRepresentation = this.Component.NonModelingRepresentationLevel == 5;
      this.ReloadPMIs = this.IsNonModelingPMILoaded;
      this.ReloadWithCommand = false;
      flag = true;
    }
    else
    {
      if (this.Component.NonModelingRepresentationLevel == 4 && this.RefrencedEntities.Count == 0)
      {
        flag = true;
        this.ReloadWithCommand = false;
      }
      if (!flag && this.CreateForm((IUiEMEndModelingForm) null).EndModeling(this))
      {
        this.MarkReferencesAsPreserved(this.RefrencedEntities.Keys);
        flag = true;
        this.ReloadWithCommand = true;
      }
    }
    this.SaveComponentPreviewImage();
    return flag;
  }

  private IUiEMEndModelingForm CreateForm(IUiEMEndModelingForm form)
  {
    form = this.RefrencedEntities.Count != 0 ? (IUiEMEndModelingForm) new CUiEMEndModelingEntityLevelForm() : (IUiEMEndModelingForm) new CUiEMEndModelingEntityLevelMiniForm();
    return form;
  }

  private void MarkReferencesAsPreserved(
    Dictionary<ITxDetailedRepresentation, bool>.KeyCollection referenceKeys)
  {
    foreach (ITxDetailedRepresentation referenceKey in referenceKeys)
    {
      if (this.RefrencedEntities[referenceKey])
        referenceKey.Preserved = true;
    }
  }

  private void SaveComponentPreviewImage()
  {
    if (!TxOptionsEx.CaptureCellPreviewImageOnEndModeling)
      return;
    new CApPCPreviewComponentApp().CreateComponentPreviewImage(this.Component);
  }

  public ITxComponent Component => this.m_component;

  public bool ReloadInDetailRepresentation
  {
    get => this.m_reloadInDetailRepresentation;
    set => this.m_reloadInDetailRepresentation = value;
  }

  public bool ReloadPMIs
  {
    get => this.m_reloadPMIs;
    set => this.m_reloadPMIs = value;
  }

  public Dictionary<ITxDetailedRepresentation, bool> RefrencedEntities => this.m_refrencedEntities;

  public TxLocalStorage GetLocalStorage
  {
    get
    {
      ITxStorable component = this.Component as ITxStorable;
      TxLocalStorage getLocalStorage = (TxLocalStorage) null;
      if (component != null)
        getLocalStorage = component.StorageObject as TxLocalStorage;
      return getLocalStorage;
    }
  }

  public bool IsNonModelingPMILoaded
  {
    get
    {
      bool modelingPmiLoaded = false;
      TxLocalStorage getLocalStorage = this.GetLocalStorage;
      if (TxStorage.op_Inequality((TxStorage) getLocalStorage, (TxStorage) null))
        modelingPmiLoaded = TxComponentEx.IsNonModelingPMILoaded(getLocalStorage);
      return modelingPmiLoaded;
    }
  }

  public bool ReloadWithCommand
  {
    get => this.m_reloadWithCommand;
    set => this.m_reloadWithCommand = value;
  }

  public bool ConsiderNonModelingStateWithoutUI
  {
    get => this.m_considerNonModelingStateWithoutUI;
    set => this.m_considerNonModelingStateWithoutUI = value;
  }
}
